﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace mylib.Utils
{
    public class Mail
    {
        private readonly string SEND_FROM = "admin@rahlin.info";
        private readonly string SEND_TO = "alexey@rahlin.info";
        private readonly string SMTP = "smtp.1and1.com";
        private readonly string CREDENTIAL_USERNAME = "admin";
        private readonly string CREDENTIAL_PASSWORD = "a24e4135";



        /// <summary>
        /// 
        /// </summary>
        /// <param name="Subject"></param>
        /// <param name="Body"></param>
        /// <returns></returns>
        public bool SendMail(string Subject, string Body)
        {
            return SendMail(Subject, Body, SEND_TO);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Subject"></param>
        /// <param name="Body"></param>
        /// <param name="SendTo"></param>
        /// <returns></returns>
        public bool SendMail(string Subject, string Body, string SendTo)
        {
            return SendMail(Subject, Body, SendTo, SEND_FROM);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Subject"></param>
        /// <param name="Body"></param>
        /// <param name="SendTo"></param>
        /// <param name="SendFrom"></param>
        /// <returns></returns>
        public bool SendMail(string Subject, string Body, string SendTo, string SendFrom)
        {
            try
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(SendFrom);
                msg.To.Add(SendTo);
                msg.Subject = Subject;
                msg.Body = Body;
                msg.IsBodyHtml = true;

                SmtpClient client = new SmtpClient(SMTP);
                client.Credentials = new NetworkCredential(CREDENTIAL_USERNAME, CREDENTIAL_PASSWORD);
                client.Send(msg);
                return true;
            }
            catch (Exception e)
            {
                throw e;
                //return false;
            }
        }

        
    }
}
